/**
 * 修改密码相关js脚本
 */

function comfirmChangePsd(which){
	$(which).attr("disabled","disabled");
	$(which).text("请求处理中…");
	$("#changePsdResult").hide();
	var text = checkData();
	if(text == "")
		return;
	var newpassword = $("#newPassword").val();
	var url=ctx+"/account/changePsd/"+newpassword;
	
	$.ajax({
		type : 'POST',
		url:url,
		contentType : 'application/json',
		data: text,
		dataType : 'text',
		success:function(data){
			$(which).removeAttr("disabled");
			$(which).text("确认保存");
			if(data=="success"){
				$("#changePsdForm").find("input[type='password']").val("");
				$(".prompt").removeClass("error");
				$(".prompt").removeClass("ok");
				$(".prompt").text("");
				$('#changePsdBlock').modal('toggle');
				setOperationSuccess();
			}else{
				$("#changePsdResult").removeClass("alert-success");
				$("#changePsdResult").addClass("alert-danger");
				$("#changePsdResult").text("修改密码失败！");
				$("#changePsdResult").show();
			}					
		},
		error:function(){
			$(which).removeAttr("disabled");
			$(which).text("确认保存");
			alert("请求异常！");
		}
	});
}

function setOperationSuccess(){
	var span = $("<span class='ok' style='margin-left:4px;'></span>");
	$("#changePsdLink").children("span").remove();
	$("#changePsdLink").append(span);
}

function checkData(){
	var result = true;
	var accountId = $("#changePsdForm").find("input[name='accountId']").val();
	var password = $("#oldPassword").val();
	var newpassword = $("#newPassword").val();
	var repassword = $("#rePassword").val();
	$(".prompt").removeClass("error");
	$(".prompt").text("");
	if(StringIsNullorEmpty(accountId) || accountId <= 0){
		result = false;
	}
	
	if(StringIsNullorEmpty(password)){
		var span = $("#oldPassword").parent().siblings(".prompt");
		setError(span,"原密码不可为空！");
		result = false;
	}
	
	if(!checkNewPassword())
		result = false;
	
	if(!checkRePassword())
		result = false;
	
	if(result)
		return JSON.stringify({accountId:accountId,password:password});
	else
		return "";
}

//异步检查当前输入原密码是否正确
function checkOldPassword(){
	var accountId = $("#changePsdForm").find("input[name='accountId']").val();
	var password = $("#oldPassword").val();
	var text = JSON.stringify({accountId:accountId,password:password});
	var url=ctx+"/account/checkPassword";
	
	$.ajax({
		type : 'POST',
		url:url,
		contentType : 'application/json',
		data: text,
		dataType : 'text',
		success:function(data){
			var span = $("#oldPassword").parent().siblings(".prompt");
			
			if(data=="success"){
				setOK(span);
			}else{
				setError(span,"原密码不正确！");
			}				
		},
		error:function(){
			alert("请求异常！");
		}
	});
}

function setOK(target){
	target.removeClass("error");
	target.addClass("ok");
	target.text("");
}

function setError(target,msg){
	target.removeClass("ok");
	target.addClass("error");
	target.text(msg);
}

function checkNewPassword(){
	var newpassword = $("#newPassword").val();
	var span = $("#newPassword").parent().siblings(".prompt");
	if(StringIsNullorEmpty(newpassword)){
		setError(span,"新密码不可为空！");
		return false;
	}else{
		setOK(span);
	}
	return true;
}

function checkRePassword(){
	var newpassword = $("#newPassword").val();
	var repassword = $("#rePassword").val();
	var span = $("#rePassword").parent().siblings(".prompt");
	if(repassword!=newpassword){
		if(StringIsNullorEmpty(repassword))
			setError(span,"请输入重复密码！");
		else
			setError(span,"密码不一致！");
		return false;
	}else{
		if(!StringIsNullorEmpty(newpassword)){
			setOK(span);
			return true;
		}	
	}
	return false;
}


function StringIsNullorEmpty(string){
	
	if(string==undefined || string =="")
		return true;
	else
		return false;
}

$(document).ready(function(){
	$('#changePsdBlock').on('show.bs.modal', function (e) {
		$("#changePsdResult").hide();
	});
	$("#oldPassword").blur(function(){
		if($("#oldPassword").val()!="")
			checkOldPassword();
	});
	
	$("#newPassword").blur(function(){
		checkNewPassword();
	});
	
	$("#rePassword").blur(function(){
		checkRePassword();
	});
});